//
//  ShapedImageView.m
//  ShapeImageView
//
//  Created by lcy on 16/1/13.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "LCYShapedImageView.h"
#import "NSBundle+Emojis.h"
#import "UIImage+Extension.h"

@interface LCYShapedImageView ()

@property (nonatomic, strong)CALayer *contentLayer; // 内容层
@property (nonatomic, strong)CAShapeLayer *maskLayer; // 蒙版层

@property (strong, nonatomic) NSMutableDictionary *mapper; // 映射表情与表情字符串

@end


@implementation LCYShapedImageView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.myTextView = [YYTextView new];
        self.myTextView.scrollEnabled = NO;
        self.myTextView.editable = NO;
        self.myTextView.backgroundColor = [UIColor lightGrayColor];
        
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

- (NSMutableDictionary *)mapper
{
    if(_mapper == nil)
    {
        self.mapper = [NSMutableDictionary dictionaryWithDictionary:[self dictionaryForPlist]];
    }
    return _mapper;
}

// 定制气泡
- (void)initBubbleViewWithSize:(CGSize)bubbleSize withBeakWidth:(CGFloat)beakWidth withLengthBetweenBeakAndTopLine:(CGFloat)length
{
    _maskLayer = [CAShapeLayer layer];
    _beakWidth = beakWidth;
    bubbleSize = CGSizeMake(bubbleSize.width - beakWidth * 2, bubbleSize.height);
    
    CGPoint origin = CGPointZero;
    CGFloat radius = 5.0;
    
    // 绘制轨迹
    UIBezierPath *currenPath = [UIBezierPath bezierPath];
    currenPath.lineCapStyle = kCGLineCapRound;
    currenPath.lineJoinStyle = kCGLineJoinRound;
    [currenPath moveToPoint:CGPointMake(origin.x + radius, origin.y)];
    NSLog(@"%lf   %lf", origin.x + radius, origin.y);
    [currenPath addLineToPoint:CGPointMake(origin.x + bubbleSize.width - radius, origin.y)];
    NSLog(@"%lf   %lf", origin.x + bubbleSize.width - radius, origin.y);
    [currenPath addArcWithCenter:CGPointMake(origin.x + bubbleSize.width - radius, origin.y + radius) radius:radius startAngle:M_PI_2 endAngle:0 clockwise:NO];
    NSLog(@"%lf   %lf", origin.x + bubbleSize.width - radius, origin.y + radius);
    
    // 右边有喙
    if(_myMessage.cellType == PostCell || _myMessage.cellType == PostAudioCell || _myMessage.cellType == PostVideoCell || _myMessage.cellType == PostFileCell || _myMessage.cellType == PostLocationCell || _myMessage.cellType == PostImageCell)
    {
        [currenPath addLineToPoint:CGPointMake(origin.x + bubbleSize.width,origin.y + length)];
        NSLog(@"%lf   %lf", origin.x + bubbleSize.width, origin.y + length);
        
        [currenPath addLineToPoint:CGPointMake(origin.x + bubbleSize.width + beakWidth,origin.y + length + beakWidth)];
        NSLog(@"%lf   %lf", origin.x + bubbleSize.width + beakWidth, origin.y + length + beakWidth);
        
        [currenPath addLineToPoint:CGPointMake(origin.x + bubbleSize.width, origin.y + length + beakWidth * 2)];
        NSLog(@"%lf   %lf", origin.x + bubbleSize.width, origin.y + length + beakWidth * 2);
    }
    
    [currenPath addLineToPoint:CGPointMake(origin.x + bubbleSize.width, origin.y + bubbleSize.height)];
    NSLog(@"%lf   %lf", origin.x + bubbleSize.width, origin.y + bubbleSize.height);
    
    [currenPath addArcWithCenter:CGPointMake(origin.x + bubbleSize.width - radius, origin.y + bubbleSize.height) radius:radius startAngle:0 endAngle:M_PI_2 clockwise:YES];
    NSLog(@"%lf   %lf", origin.x + bubbleSize.width - radius, origin.y + bubbleSize.height);
    
    [currenPath addLineToPoint:CGPointMake(origin.x + radius, origin.y + bubbleSize.height + radius)];
    NSLog(@"%lf   %lf", origin.x + radius, origin.y + bubbleSize.height + radius);
    
    [currenPath addArcWithCenter:CGPointMake(origin.x + radius, origin.y + bubbleSize.height) radius:radius startAngle:M_PI endAngle:M_PI_2 * 3 clockwise:YES];
    NSLog(@"%lf   %lf", origin.x + radius, origin.y + bubbleSize.height);
    
    // 左边有喙
    if(_myMessage.cellType == GetCell || _myMessage.cellType == GetAudioCell || _myMessage.cellType == GetVideoCell || _myMessage.cellType == GetFileCell || _myMessage.cellType == GetLocationCell || _myMessage.cellType == GetImageCell)
    {
        [currenPath addLineToPoint:CGPointMake(origin.x, origin.y + length + beakWidth * 2)];
        
        [currenPath addLineToPoint:CGPointMake(origin.x - beakWidth, origin.y + length + beakWidth)];
        
        [currenPath addLineToPoint:CGPointMake(origin.x, origin.y + length)];
    }
    
    [currenPath addLineToPoint:CGPointMake(origin.x, origin.y + radius)];
    NSLog(@"%lf   %lf", origin.x, origin.y + radius);
    [currenPath addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:NO];
    NSLog(@"%lf   %lf", radius, radius);
    
    
    _maskLayer.path = currenPath.CGPath;
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor redColor].CGColor;
    _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;
    
    
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = CGRectMake(0, 0, bubbleSize.width, bubbleSize.height);
    NSLog(@"%@", NSStringFromCGRect(_contentLayer.frame));
    [self.layer addSublayer:_contentLayer];
}

// 设置背景图
- (void)setImage:(UIImage *)image
{
    _contentLayer.contents = (id)[UIImage resizableImage:image].CGImage;
}

// 设置消息
- (void)setMyMessage:(LCYIMMessage *)myMessage
{
    _myMessage = [myMessage copy];
    
    switch (myMessage.cellType) {
        case PostCell:
        case GetCell:
        {
            [self initTextViewWithMessage:myMessage];
        }
            break;
        case GetAudioCell:
        case PostAudioCell:
        {
            [self initTextViewWithMessage:myMessage];
        }
            break;
        case PostImageCell:
        case GetImageCell:
        {
            
        }
            break;
        case PostVideoCell:
        case GetVideoCell:
        {
            
        }
            break;
        case PostFileCell:
        case GetFileCell:
        {
            
        }
            break;
        case PostLocationCell:
        case GetLocationCell:
        {
            
        }
            break;
        default:
            break;
    }
}

// 初始化文本内容
- (void)initTextViewWithMessage:(LCYIMMessage *)message
{
    _myTextView.attributedText = [[NSAttributedString alloc] initWithString:message.text attributes:nil];
    
    YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new];
    parser.emoticonMapper = self.mapper;
    
    YYTextLinePositionSimpleModifier *mod = [YYTextLinePositionSimpleModifier new];
    mod.fixedLineHeight = 18;
    
    self.myTextView.font = [UIFont systemFontOfSize:16];
    self.myTextView.textParser = parser;
    self.myTextView.linePositionModifier = mod;
    self.myTextView.textContainerInset = UIEdgeInsetsMake(10, 15, 10, 15);
    
    // 获取自适应size
    CGSize size = [self.myTextView sizeThatFits:CGSizeMake(floor(kLCYScreenWidth * 0.5), 10000)];
    _sizeOfBubble = size;
    size = CGSizeMake(floor(size.width), floor(size.height));
    
    self.myTextView.frame = CGRectMake(0, 0, size.width, size.height);
    self.myTextView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.196947030141844];
    
    // 绘制气泡框
    [self initBubbleViewWithSize:size withBeakWidth:5.0 withLengthBetweenBeakAndTopLine:10.0];
    
    [self addSubview:self.myTextView];
}


// 从plist文件读取字典
- (NSMutableDictionary *)dictionaryForPlist
{
    NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSBundle mainBundle] emojisInfoFromDirectory:@"EmoticonQQ.bundle" ofType:@"plist" ofName:@"info"]];
    
    NSMutableDictionary *diction = [NSMutableDictionary dictionary];
    for(NSDictionary *dic in array)
    {
        for(NSString *key in dic)
        {
            NSString *value = dic[key];
            UIImage *img = [[NSBundle mainBundle] emojiFromDirectory:@"EmoticonQQ.bundle" ofType:@"gif" ofIndex:value];
            
            [diction setObject:img forKey:key];
        }
    }
    return diction;
}

@end
